package com.htrq.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSON;
import com.htrq.entity.Page;
import com.htrq.entity.PoundData;
import com.htrq.service.PoundService;
import com.htrq.util.ExcelOperate;
import com.htrq.util.ExportToExcelUtil;

@Controller
@RequestMapping("jsp/owork")
public class PoundDataController {
	@Resource
	private PoundService pdService;

	/**
	 * 上传文件然后解析
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping("inputfile")
	@ResponseBody
	public String Inputfile(HttpServletRequest request, HttpServletResponse response) throws IOException,
			ParseException {
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession()
				.getServletContext());
		String fileName = "";
		// 定义上传路径
		String path = "d:/htrqms/upload";
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				if (file != null) {
					// 取得当前上传文件的文件名称
					String myFileName = file.getOriginalFilename();
					// String mm=myFileName.substring(myFileName.lastIndexOf(".") + 1);
					// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
					if (myFileName.trim() != "") {
						fileName = path + "/" + myFileName;
						// String path =
						// request.getSession().getServletContext().getRealPath("/upload");
						File localFile = new File(fileName);
						file.transferTo(localFile);
						// 删除文件使用 File file file.delete();
					}
				}
			}
		}// ----------以上是上传部分
			// 解析
		String[][] data = ExcelOperate.getData(new File(fileName), 1);
		// 验证及保存
		String result = pdService.checkandsave(data);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.print("{'success':true,'res':{'result':'" + result + "'}}");
		return null;
	}
	@RequestMapping("getAllPound")
	@ResponseBody
	public String getAllPound(PoundData pod, Page page, HttpServletResponse response) throws IOException {
		List<PoundData> list = pdService.getAllpod(pod, page);
		String str = JSON.toJSONString(list);
		Long total = pdService.getTotal(pod);
		String json = "{total:" + total + ",pods:" + str + "}";
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.write(json);
		return null;
	}
	@RequestMapping("savePoundDate")
	@ResponseBody
	public String savePoundDate(PoundData pod, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		boolean flag = pdService.modify(pod);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if (flag) {
			out.print("{'success':true,'res':{'result':'修改成功,请点刷新'}}");
		} else {
			out.print("{'success':true,'res':{'result':'修改失败'}}");
		}
		return null;
	}
	@RequestMapping("exportpd")
	public String export(PoundData pod, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String titles[] = { "流水号", "车牌号", "发货单位", "收货单位", "货物名称", "运输单位", "皮重", "毛重", "净重", "毛重时间", "净重时间", "罐车牌号" };
		List<Object[]> list = pdService.exportbyForm(pod);
		ExportToExcelUtil.exportToExcel(request, response, "磅单查询导出结果.xls", "磅单", titles, list);
		return null;
	}
}
